﻿#include "systemlogfile.h"

#include <QDebug>
#include <QApplication>
#include <QDir>
#include <QDate>
#include <QMetaEnum>
#include <QThread>
#include <QMutex>
#include <QMutexLocker>
#include <stdarg.h>

#include "filetools/filepathutils.h"
#include "tools/timeutil.h"

#define MAX_LOG_LENGH 1024

const char PATH_LogPath[] = "/logs";
const char Suffix[] = ".log";

namespace Common {

	bool SystemLogFile::m_isFileReady = false;
	bool SystemLogFile::m_isRecord2File = true;
	SystemLogFile::SYSTEMLOG_LEVEL SystemLogFile::m_logLevel = SystemLogFile::SYSTEMLOG_INFO;           //默认是info级

	QFile localFile;
	QMutex mutex;

	SystemLogFile::SystemLogFile()
	{

	}

	SystemLogFile::~SystemLogFile()
	{

	}

	/**
	 * @brief 设置系统日志等级
	 * @param level
	 */
	void SystemLogFile::setLogLevel(const SYSTEMLOG_LEVEL &level)
	{
		m_logLevel = level;
	}

	/**
	 * @brief 初始化
	 * @param logConfig
	 * @return
	 */
	bool SystemLogFile::init(SystemLogConfigInfo &logConfig)
	{
		m_isRecord2File = logConfig.isRecordFile;
		m_logLevel = (SYSTEMLOG_LEVEL)logConfig.level;

		// 创建日志文件
		QString logDir = qApp->applicationDirPath() + QString(PATH_LogPath);
		if (FilePathUtils::createDir(logDir))
		{
			QString fileName = logDir + QDir::separator() + TimeUtil::getDate() + QString(Suffix);
			localFile.setFileName(fileName);
			if (localFile.open(QFile::WriteOnly | QFile::Append | QFile::Text))
			{
				m_isFileReady = true;
			}
		}
		return m_isFileReady;
	}

	/**
	 * @brief 设置日志信息
	 * @param nLevel
	 * @param fileDesc
	 * @param functionDesc
	 * @param lineNum
	 * @param data
	 */
	void SystemLogFile::log(SYSTEMLOG_LEVEL nLevel, const char *fileDesc, const char *functionDesc, int lineNum, const char* data, ...)
	{
		QMutexLocker locker(&mutex);
		if (m_isFileReady && nLevel >= m_logLevel)
		{
			QString recordInfo = QString("[%1]").arg(Common::TimeUtil::getTimeStamp());
			recordInfo.append(getLeveDesc(nLevel));

#ifndef QT_NO_DEBUG
			recordInfo.append(QString("[%1:%2:%3]").arg(fileDesc).arg(functionDesc).arg(lineNum));
#endif
			va_list vlist;
			va_start(vlist, data);

			QByteArray byteArray;
#if defined(Q_OS_WIN)
			int recordLen = _vscprintf(data, vlist);
			byteArray.resize(recordLen);
#else
			byteArray.resize(1024);
#endif
			vsprintf(byteArray.data(), data, vlist);
			recordInfo.append(byteArray);
			va_end(vlist);

			recordInfo.append("\n");

			if (m_isRecord2File)
			{
				localFile.write(recordInfo.toLocal8Bit().data(), recordInfo.toLocal8Bit().length());
				localFile.flush();
			}
			else
			{
				qDebug() << recordInfo;
			}
		}
	}

	QString SystemLogFile::getLeveDesc(SYSTEMLOG_LEVEL level)
	{
#if (QT_VERSION > 0x050500)
		static QMetaEnum metaEnum = QMetaEnum::fromType<SYSTEMLOG_LEVEL>();
		return QString("[%1]").arg(metaEnum.key(level));
#else
		switch (level)
		{
		case SYSTEMLOG_LEVEL::SYSTEMLOG_INFO:
			return "[INFO]";
		case SYSTEMLOG_LEVEL::SYSTEMLOG_WARNING:
			return "[WARNING]";
		case SYSTEMLOG_LEVEL::SYSTEMLOG_ERROR:
			return "[ERROR]";
		}
#endif
	}

}//namespace Common
